package com.zst.dollars.handler.codec.encoder;

import com.alibaba.fastjson.JSONObject;
import com.zst.dollars.entity.Message;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class MessageToByteEncoder extends MessageToMessageEncoder<Message> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
        String jsonMessage = JSONObject.toJSONString(msg);
        ByteBuf byteBuf = ctx.alloc().buffer().writeBytes(jsonMessage.getBytes(StandardCharsets.UTF_8));
        out.add(byteBuf);
    }
}
